package com.mdd.front.service.wechat.impl;

import com.mdd.common.utils.ConfigUtil;
import com.mdd.front.config.wechat.WxMpUtil;
import com.mdd.front.service.wechat.IWechatService;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @author kn4im3
 * @date 2023/3/8 15:54
 */
@Service
public class WechatServiceImpl implements IWechatService {

    @Override
    public Map<String, Object> jsConfig(String url) throws Exception {
        Map<String, String> channel = ConfigUtil.get("oa_channel");
        String appid = channel.get("appId");
        //获取AccessToken
        String accessToken = WxMpUtil.getJSSDKAccessToken("oa_channel");
        //获取JssdkGetticket
        String jsapiTicket = WxMpUtil.getJsSdkGetTicket(accessToken);
        String timestamp = Long.toString(System.currentTimeMillis() / 1000);
        String nonceStr = UUID.randomUUID().toString();
        String signature = WxMpUtil.buildJSSDKSignature(jsapiTicket,timestamp,nonceStr,url);
        List<String> array = Arrays.asList(
                "onMenuShareTimeline",
                "onMenuShareAppMessage",
                "onMenuShareQQ",
                "onMenuShareWeibo",
                "onMenuShareQZone",
                "openLocation",
                "getLocation",
                "chooseWXPay",
                "updateAppMessageShareData",
                "updateTimelineShareData",
                "openAddress",
                "scanQRCode");
        Map<String,Object> map = new HashMap<>();
        map.put("url", url);
        map.put("jsapi_ticket", jsapiTicket);
        map.put("nonceStr", nonceStr);
        map.put("timestamp", timestamp);
        map.put("signature", signature);
        map.put("appid", appid);
        map.put("jsApiList", array);
        System.out.println(map);
        return map;
    }
}
